﻿using System.Windows;
using System.Windows.Controls;

namespace ItemsViewer
{
    /// <summary>
    /// This class is an implementation of ScrollViewer.
    /// </summary>
    public class AnimatedScrollViewer : ScrollViewer
    {      
        /// <summary>
        /// Register a CurrentVerticalOffsetProperty which has a onChange callback
        /// </summary>
        public static DependencyProperty CurrentVerticalOffsetProperty = DependencyProperty.Register("CurrentVerticalOffset", typeof(double), typeof(AnimatedScrollViewer), new PropertyMetadata(new PropertyChangedCallback(OnVerticalChanged)));

        /// <summary>
        /// Register a CurrentHorizontalOffsetProperty which has a onChange callback
        /// </summary>
        public static DependencyProperty CurrentHorizontalOffsetProperty = DependencyProperty.Register("CurrentHorizontalOffsetOffset", typeof(double), typeof(AnimatedScrollViewer), new PropertyMetadata(new PropertyChangedCallback(OnHorizontalChanged)));
        
        /// <summary>
        /// Gets or sets the current horizontal offset.
        /// </summary>
        public double CurrentHorizontalOffset
        {
            get { return (double)this.GetValue(CurrentHorizontalOffsetProperty); }
            set { this.SetValue(CurrentHorizontalOffsetProperty, value); }
        }

        /// <summary>
        /// Gets or sets the current vertical offset.
        /// </summary>
        public double CurrentVerticalOffset
        {
            get { return (double)this.GetValue(CurrentVerticalOffsetProperty); }
            set { this.SetValue(CurrentVerticalOffsetProperty, value); }
        }

        /// <summary>
        /// When the DependencyProperty is changed change the vertical offset, thus 'animating' the scrollViewer
        /// </summary>
        /// <param name="d">The DependencyObject.</param>
        /// <param name="e">The DependencyPropertyChangedEventArgs.</param>
        private static void OnVerticalChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            AnimatedScrollViewer viewer = d as AnimatedScrollViewer;
            viewer.ScrollToVerticalOffset((double)e.NewValue);
        }

        /// <summary>
        /// When the DependencyProperty is changed change the vertical offset, thus 'animating' the scrollViewer
        /// </summary>
        /// <param name="d">The DependencyObject.</param>
        /// <param name="e">The DependencyPropertyChangedEventArgs.</param>
        private static void OnHorizontalChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            AnimatedScrollViewer viewer = d as AnimatedScrollViewer;
            viewer.ScrollToHorizontalOffset((double)e.NewValue);
        }
    }
}
